import * as THREE from 'three'
import gsap from 'gsap'
import vertexShader from '@/shader/LightWall/vertex.glsl'
import fragmentShader from '@/shader/LightWall/fragment.glsl'

// 修改材质
export default class LightWall {
  constructor(radius = 5, length = 2, position = { x: 0, z: 0 }, color = 0xff0000) {
    this.geometry = new THREE.CylinderGeometry(radius, radius, 2, 32, 1, true)

    this.geometry.computeBoundingBox()
    const { min, max } = this.geometry.boundingBox
    // 获取物体的高度差
    let uHeight = max.y - min.y

    this.material = new THREE.ShaderMaterial({
      uniforms: {
        uHeight: { value: uHeight },
      },
      vertexShader,
      fragmentShader,
      transparent: true,
      side: THREE.DoubleSide,
    })
    this.mesh = new THREE.Mesh(this.geometry, this.material)
    this.mesh.position.set(position.x, 1, position.z)

    gsap.to(this.mesh.scale, {
      x: length,
      z: length,
      duration: 1,
      ease: 'none',
      repeat: -1,
      yoyo: true,
    })
  }

  remove() {
    this.mesh.remove()
    this.mesh.removeFromParent()
    this.mesh.geometry.dispose()
    this.mesh.material.dispose()
  }
}
